package com.app.service.Impl;

import com.app.enums.StatusCodeEnum;
import com.app.mapper.BrowseNumberMapper;
import com.app.mapper.UserMapper;
import com.app.model.BrowseNumberModel;
import com.app.model.UserModel;
import com.app.service.BrowseNumberService;
import com.app.util.CommonHelp;
import com.app.util.StringTool;
import com.app.util.TokenUtils;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import lombok.extern.log4j.Log4j2;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;


/**
 * @Description:访问量实现类
 * @Author: N
 * @Date: 2022年11月24日 10点29分
 */
@Service
@Log4j2
public class BrowseNumberServiceImpl implements BrowseNumberService {
    @Autowired
    private UserMapper userMapper;
    @Autowired
    private BrowseNumberMapper browseNumberMapper;

    @Override
    public int addBrowse(String token) {

        if (StringTool.isNotEmpty(token)) {
            String mobile = TokenUtils.nothingness(token);
            if (mobile == null) {
                return StatusCodeEnum.STATUS405.getCode();
            }
            QueryWrapper<UserModel> queryWrapper = new QueryWrapper<>();
            queryWrapper.select().eq("openid", mobile);
            List<UserModel> userList = userMapper.selectList(queryWrapper);
            if (userList.isEmpty()) {
                return StatusCodeEnum.STATUS406.getCode();
            }
            UserModel user = userList.get(0);
            BrowseNumberModel model = new BrowseNumberModel();
            model.setUserId(user.getId());
            model.setCrtTime(CommonHelp.getNowTimestamp());
            browseNumberMapper.insert(model);
        } else {
            BrowseNumberModel model = new BrowseNumberModel();
            model.setCrtTime(CommonHelp.getNowTimestamp());
            browseNumberMapper.insert(model);
        }

        return 200;
    }

    @Override
    public int selectCount() {
        return browseNumberMapper.selectCount(null);
    }

}
